/**
 *
 */
package hichecker.ui.components.verifier;

import hichecker.controller.Config;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 */
public class NumberVerifier extends InputVerifier{
	private JLabel errorLabel;
	private float min;
	private float max;

	public NumberVerifier(JLabel errorLabel) {
		this(0, Integer.MAX_VALUE, errorLabel);
	}

	public NumberVerifier(float min, float max, JLabel errorLabel) {
		this.errorLabel = errorLabel;
		this.min = min;
		this.max = max;
	}

	@Override
	public boolean verify(JComponent input) {
		String inputText = ((JTextField) input).getText();
		try {
			int num = Integer.parseInt(inputText);
			if (num < min) {
				errorLabel.setText(Config.POPUP_NUMBER_MIN.replaceAll("#1", "" + min));
				return false;
			}
			if (num > max) {
				errorLabel.setText(Config.POPUP_NUMBER_MAX.replaceAll("#1", "" + max));
				return false;
			}
			errorLabel.setText(null);
			return true;
		} catch (NumberFormatException e) {
			errorLabel.setText(Config.POPUP_NUMBER_NO_NUMBER);
			return false;
		}
	}
}
